//
//  PACETests.swift
//  NFCPassportReaderAppTests
//
//  Created by Andy Qua on 09/03/2021.
//  Copyright © 2021 Andy Qua. All rights reserved.
//

import Foundation
import OpenSSL
import NFCPassportReader

// Currently not actual tests - maybe at some point but currently just a holding place for some testing stuff I didn't want to get rid of!
// Hopefully will re-organise into proper tests!
class PACETests {
    func testDHKeyMapping() -> OpaquePointer {
        var resp : [UInt8] = [0x7c, 0x82, 0x01, 0x04, 0x82, 0x82, 0x01, 0x00, 0x70, 0x5a, 0xce, 0x59, 0x1c, 0x0b, 0xe1, 0xa1, 0x19, 0x5b, 0x14, 0x1c, 0x97, 0x38, 0x51, 0xde, 0x07, 0xf1, 0x66, 0xa6, 0x74, 0x7d, 0x18, 0x9f, 0x5e, 0x3b, 0x51, 0x0d, 0xbf, 0xff, 0xe2, 0x16, 0xca, 0x96, 0x0e, 0x63, 0x8d, 0x16, 0x49, 0x5f, 0x53, 0x15, 0x60, 0x9b, 0xd6, 0x6e, 0x41, 0x52, 0x69, 0x08, 0x6a, 0x37, 0x6f, 0xf4, 0xd6, 0x14, 0xcc, 0x9b, 0x9d, 0x57, 0x0e, 0x64, 0x53, 0x49, 0xa3, 0xe6, 0xd6, 0xdb, 0x80, 0xfb, 0x08, 0x39, 0xb7, 0x66, 0xdc, 0xcc, 0xb1, 0xda, 0xe0, 0xe5, 0x21, 0xaf, 0xca, 0x9b, 0xdc, 0xe2, 0xc2, 0xd9, 0xb2, 0x58, 0xd8, 0x22, 0x94, 0x23, 0x76, 0x0f, 0x40, 0x74, 0xea, 0x73, 0xf1, 0xa4, 0x4d, 0xdd, 0xfc, 0x72, 0x27, 0xda, 0x8b, 0xce, 0x03, 0x75, 0x98, 0xfe, 0x38, 0x81, 0x1d, 0x45, 0xc9, 0xd6, 0x8b, 0x81, 0x8d, 0xef, 0xdc, 0xda, 0x5f, 0xc8, 0x1a, 0x12, 0x51, 0x0d, 0xfa, 0x02, 0x67, 0xf9, 0x89, 0x5e, 0x6d, 0xb8, 0x55, 0x43, 0xb0, 0x71, 0x47, 0x0d, 0xf0, 0x27, 0xda, 0x62, 0x3b, 0xd4, 0xa9, 0x4d, 0x5e, 0xde, 0xba, 0x81, 0x7b, 0xde, 0x79, 0x5f, 0x57, 0xbf, 0x0b, 0x35, 0xf4, 0x29, 0x6f, 0x5a, 0xd4, 0xe8, 0xd9, 0xfd, 0x2c, 0x87, 0x2e, 0x84, 0x26, 0x10, 0x9d, 0x1b, 0x50, 0xe4, 0x1d, 0xb3, 0x91, 0x02, 0xac, 0xa4, 0x58, 0x52, 0x61, 0x77, 0x1f, 0x97, 0x02, 0xd6, 0x48, 0x90, 0x1f, 0x69, 0xb6, 0xa0, 0x53, 0x53, 0xf9, 0xcb, 0x57, 0x01, 0xfe, 0x9f, 0xf2, 0xa1, 0x29, 0x60, 0x3f, 0xa5, 0xa4, 0xf6, 0x3d, 0xf1, 0xc6, 0x7a, 0xf3, 0xc6, 0xfd, 0x11, 0x6c, 0x38, 0xe8, 0x91, 0x82, 0x3c, 0xd9, 0x33, 0xef, 0x57, 0x9e, 0x6c, 0x04, 0x10, 0x65, 0x6a, 0xc5, 0xaa, 0x1f, 0xc4, 0x50, 0x1d, 0xe6, 0xbd, 0x27, 0xe0, 0x79, 0x30, ]
        
        let mappingKey : OpaquePointer = EVP_PKEY_new()
        let dhKey = DH_get_2048_256()
        
        let rc = DH_generate_key(dhKey)
        EVP_PKEY_set1_DH(mappingKey, dhKey)
        
        let pubKeyData = try! unwrapDO( tag:0x7c, wrappedData:resp)
        let encodedPublicKey = try! unwrapDO(tag: 0x82, wrappedData: pubKeyData)
        
        let piccMappingPublicKey = OpenSSLUtils.decodePublicKeyFromBytes(pubKeyData: encodedPublicKey, params: mappingKey)!
        let asecret = OpenSSLUtils.computeSharedSecret(privateKeyPair: mappingKey, publicKey: piccMappingPublicKey)
        
        
        let dh_mapping_key = EVP_PKEY_get1_DH(mappingKey)
        
        
        print( "DONE" )
        
        let piccNonce : [UInt8] = [0xed, 0xb6, 0xc4, 0x07, 0x04, 0xae, 0x98, 0xfd,]
        let bn_nonce = BN_bin2bn(piccNonce, Int32(piccNonce.count), nil);
        
        let bn = BN_bin2bn(encodedPublicKey, Int32(encodedPublicKey.count), nil);
        var secret = [UInt8](repeating: 0, count: Int(DH_size(dh_mapping_key)))
        DH_compute_key( &secret, bn, dh_mapping_key)
        
        
        let bn_h = BN_bin2bn(secret, Int32(secret.count), nil);
        
        var p : OpaquePointer? = nil
        var q : OpaquePointer? = nil
        var g : OpaquePointer? = nil
        DH_get0_pqg(dh_mapping_key, &p, &q, &g);
        
        // map to new generator#imageLiteral(resourceName: "simulator_screenshot_33B42CB4-EA6E-47B4-A2A2-D96CA95B0F15.png")
        let bn_g = BN_new()
        let new_g = BN_new()
        
        // bn_g = g^nonce mod p
        // ephemeral_key->g = bn_g mod p * h  => (g^nonce mod p) * h mod p
        let bn_ctx = BN_CTX_new()
        let rc3 = BN_mod_exp(bn_g, g, bn_nonce, p, bn_ctx)
        let rc4 = BN_mod_mul(new_g, bn_g, bn_h, p, bn_ctx)
        
        
        let ephemeral_key = DHparams_dup(dh_mapping_key)
        let rc5 = DH_set0_pqg(ephemeral_key, BN_dup(p), BN_dup(q), BN_dup(new_g))
        
        BN_free( bn_nonce )
        BN_free( bn )
        BN_free(bn_h)
        BN_free(bn_g)
        BN_free(new_g)
        BN_CTX_free(bn_ctx)
        
        // Set the ephemeral params
        let ephemeralParams = EVP_PKEY_new()
        let rc6 = EVP_PKEY_set1_DH(ephemeralParams, ephemeral_key)
        
        EVP_PKEY_free(mappingKey)
        
        var ephemeralKeyPair : OpaquePointer? = nil
        let pctx = EVP_PKEY_CTX_new(ephemeralParams, nil)
        EVP_PKEY_keygen_init(pctx)
        EVP_PKEY_keygen(pctx, &ephemeralKeyPair)
        EVP_PKEY_CTX_free(pctx)
        
        EVP_PKEY_free( ephemeralParams )
        
        let publicKey = OpenSSLUtils.getPublicKeyData( from: ephemeralKeyPair! )
        print( "Ephemeral public key - \(binToHexRep(publicKey!, asArray: true))")
        
        return ephemeralKeyPair!
    }
    
    func doTest() {
        var passportEphKey : [UInt8] = [0x01, 0xd5, 0xa8, 0xb2, 0xea, 0xb6, 0x18, 0x55, 0x34, 0x24, 0xfe, 0x25, 0x10, 0x15, 0xaf, 0xc3, 0x85, 0xda, 0x13, 0x94, 0xe7, 0x2b, 0x76, 0x81, 0x2d, 0x8c, 0xe0, 0x6f, 0x60, 0x65, 0xfb, 0x33, 0x8c, 0x5b, 0x2d, 0xc9, 0x38, 0x11, 0xb3, 0x80, 0xd7, 0x49, 0xaa, 0xeb, 0x36, 0xa5, 0xdd, 0xa0, 0x78, 0x33, 0xe0, 0xd5, 0x52, 0xd4, 0x4e, 0xaa, 0xf0, 0xa3, 0x5f, 0xd3, 0x9c, 0x89, 0x44, 0xca, 0xac, 0xf1, 0x7a, 0x1e, 0xd4, 0x61, 0x58, 0x1d, 0x81, 0x83, 0xb6, 0x98, 0x85, 0x15, 0x2d, 0xeb, 0xd7, 0xc2, 0x66, 0xe4, 0xe0, 0xd1, 0x73, 0x1c, 0x52, 0xd7, 0x3a, 0x33, 0x20, 0x1f, 0x64, 0x8b, 0x85, 0xb7, 0xe5, 0xbb, 0x09, 0xe8, 0x8a, 0xd3, 0x79, 0x8c, 0x23, 0x7a, 0x35, 0xb2, 0xc5, 0xfa, 0x42, 0xcb, 0xe4, 0xc4, 0x86, 0xa7, 0x8f, 0x96, 0x22, 0xc1, 0x2b, 0x07, 0xc6, 0x29, 0xa2, 0xfd, 0x31, 0xcb, 0xba, 0x1b, 0x05, 0xca, 0xc2, 0xaa, 0xc3, 0x6f, 0x5a, 0xa8, 0x43, 0xa1, 0x60, 0x35, 0x97, 0xda, 0x35, 0xfd, 0x76, 0x16, 0xa9, 0x32, 0x91, 0xd1, 0xd8, 0x7b, 0x0b, 0xfc, 0x30, 0x99, 0x06, 0xa7, 0x62, 0x44, 0x85, 0x2b, 0xb9, 0x9b, 0x5a, 0xfb, 0x94, 0x28, 0x47, 0x40, 0x7f, 0x7a, 0x6a, 0xb2, 0xe9, 0xcc, 0xe3, 0x13, 0x52, 0x7c, 0xc0, 0x87, 0x1d, 0x72, 0xc5, 0x4b, 0x69, 0x0a, 0x2a, 0xc7, 0x37, 0xd5, 0x17, 0xd2, 0xce, 0xd2, 0x54, 0x66, 0x92, 0x42, 0x91, 0x84, 0x24, 0xef, 0xb6, 0x98, 0x6d, 0x78, 0x95, 0x0d, 0x7e, 0x49, 0xd0, 0xee, 0xcc, 0x61, 0x5d, 0x2f, 0x5a, 0xa1, 0xb2, 0xc0, 0xa9, 0xe7, 0x30, 0x94, 0x58, 0x58, 0x7a, 0x59, 0xd7, 0x96, 0xe9, 0x8c, 0x2c, 0xfd, 0xe9, 0x58, 0xe4, 0x4b, 0xa3, 0x31, 0x47, 0x03, 0x57, 0x05, 0x84, 0x6d, 0x3c, 0x4c, 0x3a, 0xaa, ];
        
        let ephKeyPair = testDHKeyMapping()
        
        let passportPublicKey = OpenSSLUtils.decodePublicKeyFromBytes(pubKeyData: passportEphKey, params: ephKeyPair)
        
        let dh = EVP_PKEY_get1_DH(ephKeyPair);
        
        var out = [UInt8](repeating:0, count:Int(DH_size(dh)))
        
        let dh_ppub = EVP_PKEY_get1_DH(passportPublicKey)
        var bn = BN_new()
        DH_get0_key( dh_ppub, &bn, nil )
        
        let len = DH_compute_key(&out, bn, dh);
        
        return
    }
}
